define(['jquery', 'bootstrap', 'backend', 'table', 'form','vue','template'], function ($, undefined, Backend, Table, Form,Vue, Template) {

    function initSearchVue() {
        var vue = new Vue({
            el:'#search_div',
            data:{
                province_list: [],
                city_list: [],
                county_list: [],
                village_list: [],
                province_id : '',
                city_id : '',
                county_id : '',
                village_id : '',
            },
            watch:{
                province_id:function (val) {
                    this.province_id = val;
                    this.getCityList();
                    this.village_list = [];
                },
                city_id:function (val) {
                    this.city_id = val;
                    this.getCountyList();
                    this.village_list = [];
                },
                county_id:function (val) {
                    this.county_id = val;
                    this.getVillageList();
                }
            },
            methods: {
                getProvinceList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : 0
                    }, function (res) {
                        that.province_list = res;
                    });
                },
                getCityList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.province_id
                    }, function (res) {
                        that.city_list = res;
                    });
                },
                getCountyList:function(){
                    var that = this;
                    $.post("adminpublic/ajax_get_region_list", {
                        parent_id : that.city_id
                    }, function (res) {
                        that.county_list = res;
                    });
                },
                getVillageList:function(){
                    var that = this;
                    $.get("adminpublic/ajax_village_list", {
                        county_id : that.county_id
                    }, function (res) {
                        that.village_list = res;
                    });
                }
            }
        });
        vue.getProvinceList();
        return vue;
    }

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'repair/index',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search:false,
                showExport:false,
                columns: [
                    [
                        {field: 'state', checkbox: true },
                        {field: 'id', title: 'ID',searchable:false},
                        {field: 'user_nickname', title: __('User name'), searchable:false},
                        {field: 'village_name', title: __('Village name'), searchable:false},
                        {field: 'detail', title: __('Address'), searchable:false},
                   /*     {field: 'province', title: __('Province'),visible:false,searchList: function (column) {
                                return Template('province_search', {});
                            }
                        },
                        {field: 'city', title: __('City'),visible:false,searchList: function (column) {
                                return Template('city_search', {});
                            }
                        },
                        {
                            field: 'county', title: __('County'), visible: false, searchList: function (column) {
                                return Template('county_search', {});
                            }
                        },*/
                        {field: 'village_id', title: __('Village'),visible:false,searchList: function (column) {
                                return Template('village_search', {});
                            }
                        },
                        {field: 'create_time', title: __('Create time'),
                            formatter: Table.api.formatter.datetime, operate: 'BETWEEN',
                            type: 'datetime', addclass: 'datetimepicker',
                            data: 'data-date-format="YYYY-MM-DD"',
                            searchable:false
                        },
                        {field: 'status', title: __('Status'),events: Controller.api.events.deal,formatter:function(value,row,index){
                                value = '' + value;
                                switch (value){
                                    case '0':
                                        // return __('Deal wait');
                                        return '<a class="btn btn-default btn-xs btn-dealed">' + __('Deal wait') + '</a>'
                                    case '1':
                                        // return __('Dealed');
                                        return '<a class="btn btn-default btn-xs btn-deal">' + __('Processing') + '</a>'
                                    case '2':
                                        // return __('Dealed');
                                        return '<a class="btn btn-default btn-xs btn-dealed">' + __('Dealed') + '</a>'
                                }
                            },searchList: {'0': __('Deal wait'), '1': __('Processing'), '2': __('Dealed')}, style: 'min-width:100px;'

                        },
                        {field: 'remark', title: __('Remark'), searchable:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                          /*  buttons: [
                                {name: 'detail', text: 'Ajax', id:'a', title: 'Ajax标题', icon: 'fa fa-flash', classname: 'btn btn-xs btn-success btn-deal'}
                                ],*/
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $(document).on("click", ".btn-export", function () {
                var ids = [];
                var selectdList = table.bootstrapTable('getSelections');
                for(var i = 0; i < selectdList.length; i++){
                    ids.push(selectdList[i]['id']);
                }
                var status = $('[name="status"]').val();
                var villageId = $('[name="village_id"]').val();
                console.info(status);
                window.location.href='repair/exportRepair?ids='+ids+'&status='+status+'&village_id='+villageId;
            })
            initSearchVue();
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
        },
        api: {
            events: {
                deal:{
                    'click .btn-deal': function (e, value, row, index) {
                        Layer.confirm("是否确认处理", function () {
                            $.ajax({
                                type:"post"
                                ,url:"repair/deal"
                                ,data:{
                                    id:row.id
                                }
                                ,error:function (data) {
                                    console.info(data);
                                    Layer.msg("服务器异常",{icon:2});
                                }
                                ,success:function (data) {
                                    console.info(data);
                                    if(data.status == 1){
                                        Layer.msg(data.msg, {icon:1}, function () {
                                            $("#table").bootstrapTable('refresh');
                                        });
                                    }
                                    else{
                                        Layer.msg(data.msg, {icon:2});
                                    }
                                }
                            });
                        });
                    }
                }
            }
        }
    };
    return Controller;
});